package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.DateUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.health.pojo.User;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    //依赖注入
    @Reference
    private UserService userService;

    @RequestMapping("/findMenu")
    public Result findMenu(String username){
        try {
            List<Menu> menuList = userService.findMenu(username);
            return new Result(true,MessageConst.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
    }

    /**
     * 获取用户名
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //再从 认证对象中 获取授权框架用户对象User
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
            //用户对象User 获取 用户名
            String username = user.getUsername();
            return new Result(true, MessageConst.ACTION_SUCCESS, username);
        } catch (Exception e) {
            e.printStackTrace();
            //获取失败,返回失败信息
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 登录成功
     * @return
     */
    @RequestMapping("/loginSuccess")
    public Result loginSuccess(){
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

    /**
     * 登录失败
     * @return
     */
    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false,"用户名或密码输入错误,请重新输入");
    }

    @RequestMapping("/login")
    public Result login(String username, String password){
        System.out.println("oms backend====u:"+username+" ,p:"+password);
        if (userService.login(username, password)){
            System.out.println("login ok!!!");
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } else {
            System.out.println("login fail");
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult =userService.findAll(queryPageBean);

            List userlist = pageResult.getRows();
            PageInfo<User> pageInfo = new PageInfo<User>(userlist);
            List<User> list = pageInfo.getList();

            for (User user : list) {
                Date date1 = user.getBirthday();
                System.out.println("从数据库返回的日期是："+date1);
                String s = DateUtils.parseDate2String(date1, "MM月-dd日");
                System.out.println("要显示的数据是："+s);
                user.setWebBirthday(s);
            }

            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody User user,Integer[] roleIds){
        try {

            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String s = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(s);

            userService.add(user,roleIds);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            User user = userService.findById(id);
            Date userBirthday = user.getBirthday();
            String editBirthday = DateUtils.parseDate2String(userBirthday, "MM-dd");
            user.setWebBirthday(editBirthday);
            return new Result(true,MessageConst.ACTION_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("findroleIdsByuserId")
    public Result findroleIdsByuserId(Integer id){
        try {
            List<Integer> roleIds = userService.findroleIdsByuserId(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody User user,Integer[] roleIds){
        try {
            userService.edit(user,roleIds);

            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("deleteRoleIdByUserId")
    public Result deleteByUserId(Integer id){
        try {
            userService.deleteRoleIdByUserId(id);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try {
            userService.deleteById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
